package com.buddy.sds.auth.controller;

import com.buddy.sds.auth.entity.DepartmentTree;
import com.buddy.sds.auth.entity.SystemDepartment;
import com.buddy.sds.auth.service.ISystemDepartmentService;
import com.buddy.sds.common.RestApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/auth/department")
public class SystemDepartmentController {

    @Autowired
    private ISystemDepartmentService departmentService;

    @GetMapping("/getDepartmentList")
    public RestApiResponse getDepartmentList(){

        List<DepartmentTree> departments = departmentService.getDepartmentTree();
        return new RestApiResponse(0,"",departments);
    }
    @PostMapping("/addDepartment")
    public RestApiResponse addDepartment(@RequestBody SystemDepartment department){

        int status = -1;
        String message = null;

        do{

            if(StringUtils.isEmpty(department.getDepartmentName())){

                message = "部门名称为空";
                break;
            }
            if(StringUtils.isEmpty(department.getDepartmentPid())){
                //根节点为ROOT
                department.setDepartmentPid("ROOT");
            }

            if(departmentService.save(department)){
                status = 0;
                message = "添加成功";
            }else{
                message = "添加失败";
            }

        }while (false);

        return new RestApiResponse(status,message,null);
    }

    /**
     * 更新部门信息
     * @param department
     * @return
     */
    @PutMapping("/updateDepartment")
    public RestApiResponse updateDepartment(@RequestBody SystemDepartment department){

        int status = -1;
        String message = null;

        do{
            if(StringUtils.isEmpty(department.getDepartmentName())){
                message = "部门id为空";
                break;
            }

            SystemDepartment oldDepartment = departmentService.getById(department.getDepartmentId());

            if(oldDepartment == null){
                message = "部门不存在";
                break;
            }

            if(StringUtils.isEmpty(department.getDepartmentName())){

                message = "部门名称为空";
                break;
            }

            if(StringUtils.isEmpty(department.getDepartmentPid())){
                //根节点为ROOT
                department.setDepartmentPid("ROOT");
            }else{
                //检查父节点是否存在
                if( !departmentService.idExists(department.getDepartmentPid())){
                    message = "父节点不存在";
                    break;
                }

                //检查当前配置的父id是不是当前id的子孙节点
                //防止出现自循环
                if(departmentService.isChildNode(department.getDepartmentId(),department.getDepartmentPid())){
                    message = "父节点不能为当前节点的后代节点";
                    break;
                }
            }
            //创建时间不能变
            department.setDepartmentCreatetime(oldDepartment.getDepartmentCreatetime());


            if(departmentService.updateById(department)){
                status = 0;
                message = "更新成功";
            }else{
                message = "更新失败";
            }

        }while (false);

        return new RestApiResponse(status,message,null);

    }

    /**
     * 获取部门信息
     * @param depid
     * @return
     */
    @GetMapping("/getDepartment")
    public RestApiResponse getDepartment(@RequestParam("departmentId")String depid){

        SystemDepartment department = departmentService.getById(depid);

        return new RestApiResponse(department == null ? -1 : 0 ,department == null ? "部门不存在":"",department);
    }

    /**
     * 级联删除部门 会同时删除子部门 部门相关用户的部门信息会被置空
     * @param depid
     * @return
     */
    @DeleteMapping("/deleteDepartment")
    public RestApiResponse deleteDepartment(@RequestParam("departmentId")String depid){

        departmentService.deleteCascase(depid);

        return new RestApiResponse(0,"删除成功",null);
    }


}
